using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Perovich.GameObjects.Controls.Shapes
{
    /// <summary>
    /// The <see cref="Line"/> class draws a colored line from one point to another.
    /// </summary>
    public class Line : Control
    {
        Vector2 _position2;
        private Texture2D lineTex;

        /// <summary>
        /// Gets or sets the texture used to draw the line.
        /// </summary>
        protected Texture2D LineTex
        {
            get { return lineTex; }
            set { lineTex = value; }
        }

        /// <summary>
        /// The seccond endpoint of the <see cref="Line"/>.
        /// </summary>
        public Vector2 Position2
        {
            get
            {
                return _position2;
            }
            set
            {
                _position2 = value;
            }
        }


        /// <summary>
        /// Constructs a new instance of the <see cref="Line"/> class.
        /// </summary>
        /// <param name="game">
        ///     <see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="Line"/> will be constructed for.
        /// </param>
        public Line(Interfaces.ICanHoldComponents game)
            : base(game)
        {
        }


        /// <summary>
        /// Loads the content for the <see cref="Line"/> class.
        /// </summary>
        protected override void LoadContent()
        {
            lineTex = Perovich.GameObjects.Helpers.Textures.GetSolidcolorTexture(GraphicsDevice);
            base.LoadContent();
        }


        /// <summary>
        /// Allows the <see cref="Line"/> to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game's timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            Vector2 P0 = Position;
            Vector2 P1 = Position2;

            Vector2 delta = P1 - P0;
            float rho = delta.Length();
            float theta = (float)Math.Atan2(delta.Y, delta.X);


            SBatches.CurrentBatch.Draw(lineTex, new Microsoft.Xna.Framework.Rectangle((int)P0.X, (int)P0.Y, (int)rho, 1), null, ForegroundColor, theta, Vector2.Zero, SpriteEffects.None, Depth);

            base.Draw(gameTime);
        }
    }
}
